package com.sskj.b70.v.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.b70.MyAppLication;
import com.sskj.b70.R;
import com.sskj.b70.p.base.BasePresenter;
import com.sskj.b70.v.activity.LoginActivity;
import com.sskj.b70.v.activity.VerifyIdentActivity;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.sskj.b70.common.Constants.VERIFY_IDENT_CODE;

/**
 * Created by Lee on 2018/2/28 0028.
 */

public abstract class BaseFragment<T extends BaseActivity, P extends BasePresenter> extends Fragment implements IBaseView {

    public T mActivity;
    protected P mPresenter;
    private Unbinder mUnbinder;
    private View inflate;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        RudenessScreenHelper.resetDensity(getContext(), 750);
        if (inflate == null) {
            inflate = inflater.inflate(getLayoutId(), null);
            mActivity = (T) getActivity();
            mPresenter = getPresenter();
            mPresenter.attachView(this, mActivity);
            mUnbinder = ButterKnife.bind(this, inflate);
            initView();
            initData();
        }
        if (inflate.getParent() != null) {
            ((ViewGroup) inflate.getParent()).removeView(inflate);
        }
        return inflate;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter.cancelRequest();
        }
        mUnbinder.unbind();
    }

    public abstract int getLayoutId();

    public abstract P getPresenter();

    public void initView() {
    }

    public void initData() {
    }


    public void setText(TextView view, String text) {
        if (view != null && text != null) {
            view.setText(text);
        }
    }

    /**
     * 判断用户实名认证
     */
    protected boolean checkIdent() {
        if (MyAppLication.getRealnameStatus() == 3) {//已实名认证
            return true;
        } else if (MyAppLication.getRealnameStatus() == 2) {//审核中
            showApplyTip(getString(R.string.base_fragment_ident_watting), false);
            return false;
        } else if (MyAppLication.getRealnameStatus() == 4) {//被拒绝
            showApplyTip(getString(R.string.base_fragment_ident_reident), true);
            return false;
        } else {
            showApplyTip(getString(R.string.base_fragment_ident_restart), true);
            return false;
        }
    }

    /**
     * 判断用户实名认证弹窗
     */
    private void showApplyTip(String msg, boolean status) {
        AlertDialog tipDialog = new AlertDialog.Builder(mActivity)
                .setTitle(R.string.base_fragment_tip)
                .setMessage(msg)
                .setPositiveButton(status ? getString(R.string.base_fragment_to_ident) : getString(R.string.base_fragment_know), (dialog, which) -> {
                    dialog.dismiss();
                    if (status) {
                        openActivity(VerifyIdentActivity.class, VERIFY_IDENT_CODE);
                    }
                })
                .setNegativeButton(getString(R.string.base_fragment_cancle), (dialog, which) -> dialog.dismiss())
                .create();
        tipDialog.show();
    }

    protected void openActivity(Class<? extends Activity> clz, int requestCode) {
        if (MyAppLication.isLogin()) {
            startActivityForResult(new Intent(mActivity, clz), requestCode);
        } else {
            startActivity(new Intent(mActivity, LoginActivity.class));
        }
    }

}
